package com.guigu.erp.service.impl;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.mapper.DesignFileMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * ClassName:DesignFileServiceImpl
 * Package:com.guigu.erp.service.impl
 * Description:
 *
 * @Date:2024/2/21 19:14
 * @Author:chenchong
 */
@Service
public class DesignFileServiceImpl extends ServiceImpl<DesignFileMapper, DesignFile>
        implements DesignFileService {

    @Autowired
    private DesignFileMapper designFileMapper;

    @Override
    public PageUtils<DesignFile> page(DesignFileVo vo, int pageSize, int pageNo) {
        QueryWrapper<DesignFile> qw = new QueryWrapper<DesignFile>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_type())) {
                qw.eq("type", vo.getSearch_type());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("register_time", vo.getSearch_registerTime2());
            }
        }
        qw.eq("CHECK_TAG","S001-1");
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> list = this.list(qw);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(list);
        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }
    @Override
    public PageUtils<DesignFile> page1(DesignFileVo vo, int pageSize, int pageNo) {
        QueryWrapper<DesignFile> qw = new QueryWrapper<DesignFile>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("register_time", vo.getSearch_registerTime2());
            }
        }
        qw.eq("CHECK_TAG","S001-1");
        qw.eq("DELETE_TAG","C001-0");
        qw.eq("DESIGN_CELL_TAG","K001-0");
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> list = this.list(qw);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(list);
        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }
    @Override
    public PageUtils<DesignFile> page2(DesignFileVo vo, int pageSize, int pageNo) {
        QueryWrapper<DesignFile> qw = new QueryWrapper<DesignFile>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
        }
        qw.eq("CHECK_TAG","S001-1");
        qw.eq("DELETE_TAG","C001-0");
        qw.eq("type","Y001-2");
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> list = this.list(qw);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(list);
        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> unmakePage(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        // 额外添加4个条件
        // 类型是商品
        wrapper.eq("type", "Y001-1");
        // 审核状态：审核通过
        wrapper.eq("check_tag", "S001-1");
        // 删除状态：未删除
        wrapper.eq("delete_tag", "C001-0");
        // 物料设计标志：未设计
        wrapper.eq("design_module_tag", "W001-0");

        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(wrapper);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(designFileList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> designReview1(int pageNo, int pageSize) {
        QueryWrapper<DesignFile> qw = new QueryWrapper<>();
        qw.eq("CHECK_TAG", "S001-0");
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> list = this.list(qw);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(list);

        PageUtils<DesignFile> pageUtils = new PageUtils<>();

        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public String getMaxFlowNumber() {
        DesignFile designFile = designFileMapper.queryMaxFile();
        if(designFile==null){
            return null;
        }
        // 获取产品编号
        String productId = designFile.getProductId();

        // 截取后6位流水号
        String flowNumber = productId.substring(9);
        return flowNumber;
    }

    @Override
    public PageUtils<DesignFile> moduleFileList(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> qw = new QueryWrapper<DesignFile>();
        //未删除
        qw.eq("DELETE_TAG", "C001-0");
        //物料
        qw.eq("TYPE", "Y001-2");
        //已审核
        qw.eq("CHECK_TAG", "S001-1");
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFiles = designFileMapper.selectList(qw);
        PageInfo<DesignFile> p = new PageInfo<DesignFile>(designFiles);

        PageUtils<DesignFile> utils = new PageUtils<DesignFile>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @Override
    public PageUtils<DesignFile> moduleFileList2(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> qw = new QueryWrapper<DesignFile>();
        //未删除
        qw.eq("DELETE_TAG", "C001-0");
        //物料
        qw.eq("TYPE", "Y001-1");
        //已审核
        qw.eq("CHECK_TAG", "S001-1");
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFiles = designFileMapper.selectList(qw);
        PageInfo<DesignFile> p = new PageInfo<DesignFile>(designFiles);

        PageUtils<DesignFile> utils = new PageUtils<DesignFile>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }
    @Override
    public PageUtils<DesignFile> unmakePage1(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        // 额外添加4个条件
        // 类型是商品
        wrapper.eq("type", "Y001-1");
        // 审核状态：审核通过
        wrapper.eq("check_tag", "S001-1");
        // 删除状态：未删除
        wrapper.eq("delete_tag", "C001-0");
        // 工序设计标志：未设计
        wrapper.eq("DESIGN_PROCEDURE_TAG", "G001-0");

        if (vo !=
                null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(wrapper);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(designFileList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }
}
